﻿using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace demo1
{
    public class MainViewModel
    {
        private bool _button2Enabled = true;

        public ICommand Button2Command { get; }
        public ICommand Button3Command{ get; }
        public ICommand Button4Command { get; } // 示例：按钮4命令，始终可用

        public MainViewModel()
        {
            // Button2Command = new RelayCommand(_ => OnButton2(),_ => Button2Enabled);
            Button2Command = new RelayCommand(OnButton2, _ => Button2Enabled);
            Button3Command = new RelayCommand(OnButton3);
            Button4Command = new RelayCommand(OnButton4, _ => true); // 示例：按钮4命令，始终可用
        }

        /// <summary>
        /// 按钮2命令, 单击后禁用按钮。
        /// 实现的委托：_ => OnButton2()
        /// 委托的原型： public delegate void Action<in T>(T obj);
        /// </summary>
        private void OnButton2(object parameter)
        {
            MessageBox.Show($"按钮2命令已触发！{parameter}", "提示");
            Button2Enabled = false; // 单击后禁用按钮
        }

        private void OnButton3(object parameter)
        {
            MessageBox.Show($"按钮2命令已触发！{parameter}", "提示");
        }

        /// <summary>
        /// Handles the action triggered by Button 4.
        /// </summary>
        /// <param name="parameter">An optional parameter passed to the button command. This value can be used to provide additional context or
        /// data for the action.这里的参数必须是object类型
        /// </param>
        private void OnButton4(object parameter)
        {
            MessageBox.Show($"按钮4命令已触发！{parameter}", "提示");
        }

        public bool Button2Enabled
        {
            get => _button2Enabled;
            set
            {
                if (_button2Enabled != value)
                {
                    _button2Enabled = value;
                    OnPropertyChanged();
                    // 通知命令状态改变
                    CommandManager.InvalidateRequerySuggested();
                }
            }
        }

        

        private string _textBoxContent;
        public string TextBoxContent
        {
            get => _textBoxContent;
            set
            {
                if (_textBoxContent != value)
                {
                    _textBoxContent = value;
                    OnPropertyChanged();
                    // 这里处理内容改变逻辑
                    // 例如：
                    MessageBox.Show("内容已变更: " + _textBoxContent);
                }
            }
        }
        public string _textBoxContent2;
        public string TextBoxContent2
        {
            get => _textBoxContent2;
            set
            {
                if (_textBoxContent2 != value)
                {
                    _textBoxContent2 = value;
                    OnPropertyChanged();
                    // 这里处理内容改变逻辑
                    // 例如：
                    MessageBox.Show("内容已变更2: " + _textBoxContent2);
                }
            }
        }

        public string _textBoxContent3;
        public string TextBoxContent3
        {
            get => _textBoxContent3;
            set
            {
                if (_textBoxContent3 != value)
                {
                    _textBoxContent3 = value;
                    OnPropertyChanged();
                    // 这里处理内容改变逻辑
                    // 例如：
                    MessageBox.Show("内容已变更3: " + _textBoxContent3);
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    /// <summary>
    ///  命令基类,实现ICommand接口
    /// </summary>
    public class RelayCommand : ICommand
    {
        private readonly Action<object> _execute; // 执行命令的委托
        private readonly Predicate<object> _canExecute; // 确定命令是否可以执行的委托

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="execute">执行命令的委托</param>
        /// <param name="canExecute">确定命令是否可以执行的委托</param>
        public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter);
        public void Execute(object parameter) => _execute(parameter);

        /// <summary>
        /// 命令状态改变时触发
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add => CommandManager.RequerySuggested += value; // 订阅命令状态改变事件 
            remove => CommandManager.RequerySuggested -= value; // 取消订阅命令状态改变事件
        }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }

        /// <summary>
        /// Button click event handler.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Button clicked!", "Notification", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        /// <summary>
        /// TextBox TextChanged event handler that updates the ViewModel property.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // 这里处理内容改变逻辑
            MessageBox.Show("TextBox 内容已改变: " + textBox.Text);
        }
        /// <summary>
        /// Button2 click event handler that updates the source of textBox3's binding.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            // 获取 textBox3 的 Text 属性的 BindingExpression
            var binding = textBox3.GetBindingExpression(TextBox.TextProperty);
            if (binding != null)
            {
                binding.UpdateSource(); // 手动将 TextBox 的内容更新到 ViewModel
            }
        }
    }
}